# -*- coding: utf-8 -*-
'''eof
name:申请人当前资产处置账户数
code:IC12_IndCurrAsseDispCnt
tableName:
columnName:
groups:行内二代个人征信数据
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:申请人当前资产处置账户数
eof'''



def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
    获取表信息
    :param data:表上层的字典对象
    :param tableName:表的名字
    :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
    :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
    :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
    '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]
    
def isFloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) >= 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def IC12_IndCurrAsseDispCnt():
    data = IC_DSJ_2D.get('data')
    # 信贷交易信息概要,出现次数[1..1],下级字段出现次数[0..1]
    PCO = getTable(data, 'PCO', 1, 0)
    if PCO == [{}]:
        return u'缺失值'
    elif PCO == 0:
        return 0
    else:
        #信贷交易信息概要信息单元,出现次数[0..1]，下级字段出现次数[0..1]
        PC02 = getTable(PCO[0], 'PC02', 0, 0)
        if PC02 == [{}]:
            return u'缺失值'
        elif PC02 == 0:
            return 0
        else:
            #被追偿汇总信息段，出现次数[0..1]，下级字段出现次数[1..2]
            PC02B = getTable(PC02[0], 'PC02B', 0, 1)
            if PC02B == [{}]:
                return u'缺失值'
            elif PC02B == 0:
                return 0
            else:
                #被追偿汇总信息，出现次数[1..2]，下级字段出现次数[1..1]
                PC02BH = getTable(PC02B[0], 'PC02BH', 1, 1)
                if PC02BH == [{}]:
                    return u'缺失值'
                elif PC02BH == 0:
                    return 0
                else:
                    cnts = 0
                    countCnts = 0
                    for i in PC02BH:
                        try:    
                            PC02BD01 = i.get('PC02BD01')
                            PC02BS03 = i.get('PC02BS03')
                            if isFloat(PC02BD01) and isFloat(PC02BS03): #判断字段值都必须可转为数值，否则记为异常
                                if PC02BD01 in [1,"1"]:
                                    countCnts += float(PC02BS03)
                            else:
                                cnts += 1
                        except:
                            cnts += 1  #单条遍历失败则记为异常
                    if cnts == len(PC02BH):  #如果每一次遍历都失败，则返回异常值
                        return u'缺失值'
                    else:
                        return int(countCnts)
result = IC12_IndCurrAsseDispCnt()
